package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.zd.demo1.sql.UserDBHelper;
import cn.zd.demo1.sql.entity.TabUserInfo;
import cn.zd.demo1.util.StringUtil;

public class MyContentProviderActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String DB_TABLE_NAME="tbl_cp_userinfo";

    private TextView tv_0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_contentprovider);
        Toast.makeText(this,this.getClass().getName(),Toast.LENGTH_LONG).show();
        findViewById(R.id.btn_0).setOnClickListener(this);
        tv_0 = findViewById(R.id.tv_0);
    }

    @SuppressLint("Range")
    @Override
    public void onClick(View v) {
        UserDBHelper helper = UserDBHelper.getInstance(this, 1);
        Cursor query = helper.openRead().query(DB_TABLE_NAME, null, null, null, null, null, null);
        List<TabUserInfo> userInfos = new ArrayList<>();
        while (query.moveToNext()) {
            TabUserInfo info = new TabUserInfo();
            info.setUserId(query.getInt(query.getColumnIndex("user_id")));
            info.setUserName(query.getString(query.getColumnIndex("user_name")));
            info.setUserAge(query.getInt(query.getColumnIndex("user_age")));
            userInfos.add(info);
        }

        Toast.makeText(this, "查询所有用户数:" + userInfos.size(), Toast.LENGTH_SHORT).show();
        tv_0.setText("当前数据\n" + StringUtil.listToString(userInfos));
    }
}